#include "expresultspectralframe.h"
#include "ui_expresultspectralframe.h"
#include <QColorDialog>
#include "algorithm.h"
#include"delegate.h"

ExpResultSpectralFrame::ExpResultSpectralFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpResultSpectralFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;
    pExpConllectPara=pExperiment->GetExpConllectParaPtr();

    CheckBox *pCheckBox;
    pCheckBox=new CheckBox(this,1);//
    ui->treeWidget_Spectral->setItemDelegateForColumn(1,pCheckBox);
    ui->treeWidget_Spectral->setColumnHidden(3,true);

    pQwtPlotSpectral=new MyQwtPlot;
    ui->verticalLayout_Spectral->insertWidget(0,pQwtPlotSpectral);

    LoadData();
}

ExpResultSpectralFrame::~ExpResultSpectralFrame()
{
    delete ui;
}

void ExpResultSpectralFrame::LoadData()
{
    disconnect(ui->treeWidget_Spectral,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
            this,SLOT(OnTreeSpectralItemChanged(QTreeWidgetItem*,int)));
    disconnect(ui->treeWidget_Spectral,SIGNAL(itemPressed(QTreeWidgetItem*,int)),
            this,SLOT(OnTreeSpectralItemPressed(QTreeWidgetItem*,int)));

    ui->treeWidget_Spectral->clear();
    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    QStringList colorNames = QColor::colorNames();
    int colorIndex=0;
    for(int i=0;i<sampleInfors.count();i++)
    {
        SampleInfor *pSampleInfor=pExpSample->GetSampleInforPtr(i);
        SampleInfor sampleInfor=sampleInfors.at(i);
        QTreeWidgetItem *pItemSample=new QTreeWidgetItem(ui->treeWidget_Spectral);
        pItemSample->setText(0,sampleInfor.sampleName);
        pItemSample->setText(1,"3");
        pItemSample->setText(3,QString::number(i));
        ui->treeWidget_Spectral->addTopLevelItem(pItemSample);
        for(int j=0;j<sampleInfor.preScanCount;j++)
        {
            QString name=QString("预扫描%1").arg(j+1);
            QTreeWidgetItem *pItemScan=new QTreeWidgetItem(pItemSample);
            pItemScan->setText(0,name);
            pItemScan->setText(1,"0");
            QColor color;
            while(1)
            {
                if(colorIndex>=colorNames.count()) colorIndex=0;
                QColor color1(colorNames[colorIndex++]);
                if(color1.blue()<200&&color1.red()<200&&color1.green()<200)
                {
                   color =color1;
                   break;
                }
            }
            pItemScan->setData(2,Qt::DecorationRole,color);

            pSampleInfor->GetPreScanResultPtr(j)->scanState.bIsShow=false;
            pSampleInfor->GetPreScanResultPtr(j)->scanState.color=color;
        }
        for(int j=0;j<sampleInfor.mainScanCount;j++)
        {
            QString name=QString("主扫描%1").arg(j+1);
            QTreeWidgetItem *pItemScan=new QTreeWidgetItem(pItemSample);
            pItemScan->setText(0,name);
            pItemScan->setText(1,"0");
            QColor color;
            while(1)
            {
                if(colorIndex>=colorNames.count()) colorIndex=0;
                QColor color1(colorNames[colorIndex++]);
                if(color1.blue()<200&&color1.red()<200&&color1.green()<200)
                {
                   color =color1;
                   break;
                }
            }
            pItemScan->setData(2,Qt::DecorationRole,color);
            pSampleInfor->GetMainScanResultPtr(j)->scanState.bIsShow=false;
            pSampleInfor->GetMainScanResultPtr(j)->scanState.color=color;
        }
    }

    connect(ui->treeWidget_Spectral,SIGNAL(itemChanged(QTreeWidgetItem*,int)),
            this,SLOT(OnTreeSpectralItemChanged(QTreeWidgetItem*,int)));
    connect(ui->treeWidget_Spectral,SIGNAL(itemPressed(QTreeWidgetItem*,int)),
            this,SLOT(OnTreeSpectralItemPressed(QTreeWidgetItem*,int)));

    ShowSpectralPlot();
}

void ExpResultSpectralFrame::OnTreeSpectralItemChanged(QTreeWidgetItem* pItem,int column)
{
    //
    QTreeWidgetItem* pParentItem=pItem->parent();
    int sampleIndex=pParentItem->text(3).toInt();
    pExpSample=pExperiment->GetExpSamplePtr();
    SampleInfor *pSampleInfor=pExpSample->GetSampleInforPtr(sampleIndex);
    QString scanName=pItem->text(0);
    if(scanName.contains("预扫描"))
    {
        int scanIndex=scanName.remove("预扫描").toInt()-1;
        pSampleInfor->GetPreScanResultPtr(scanIndex)->scanState.bIsShow=pItem->text(1).toInt();
        pSampleInfor->GetPreScanResultPtr(scanIndex)->scanState.color=pItem->data(2,Qt::DecorationRole).value<QColor>();
    }
    else if(scanName.contains("主扫描"))
    {
        int scanIndex=scanName.remove("主扫描").toInt()-1;
        pSampleInfor->GetMainScanResultPtr(scanIndex)->scanState.bIsShow=pItem->text(1).toInt();
        pSampleInfor->GetMainScanResultPtr(scanIndex)->scanState.color=pItem->data(2,Qt::DecorationRole).value<QColor>();
    }

    ShowSpectralPlot();
}

void ExpResultSpectralFrame::OnTreeSpectralItemPressed(QTreeWidgetItem* pItem,int column)
{
    if(column!=2) return;
    if(pItem->text(1)=="3") return;

    QColor color=QColorDialog::getColor();
    if(color.isValid()==false) return;
    pItem->setData(2,Qt::DecorationRole,color);
}


void ExpResultSpectralFrame::ShowSpectralPlot()
{
    foreach(QwtPlotCurve *pCurve,curves)
    {
        delete pCurve;
    }
    curves.clear();

    pExpSample=pExperiment->GetExpSamplePtr();
    QList<SampleInfor> sampleInfors=pExpSample->GetSampleInfors();
    for(int m=0;m<sampleInfors.count();m++)
    {
        SampleInfor sampleInfor=sampleInfors.at(m);
        if(sampleInfor.scanState!=2) continue;//
        for(int n=0;n<sampleInfor.preScanCount;n++)
        {
            if(pExpConllectPara->bPreScan==false) break;
            QList<ScanPara> scanParas;
            pExperiment->GetScanPara(scanParas,0);
            QColor color=sampleInfor.GetPreScanResultPtr(n)->scanState.color;
            bool state=sampleInfor.GetPreScanResultPtr(n)->scanState.bIsShow;
            rawDatas=sampleInfor.GetPreScanResultPtr(n)->GetPreExpRawData();
            int index=0;
            for(int i=0;i<scanParas.count();i++)
            {
                int channel=scanParas.at(i).channels;
                double startMass=scanParas.at(i).startMassNum;
                double interval=scanParas.at(i).interval;

                QwtPlotCurve *pCurve = new QwtPlotCurve;
                pCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
                pCurve->attach(pQwtPlotSpectral);
                pCurve->setVisible(state);
                curves.append(pCurve);

                pCurve->setPen(color);
                pCurve->setStyle(QwtPlotCurve::Lines);
                //
                QList<double> xs=Algorithm::GetNumList(channel,startMass,interval);
                QList<double> ys;
                for(int j=0;j<channel;j++)
                {
                    if(index>=rawDatas.count()) break;
                    ys.append(rawDatas.at(index).plusedValue);
                    //ys.append(rawDatas.at(index).analogValue);
                    index++;
                }
                pCurve->setSamples(xs.toVector(),ys.toVector());
            }
        }
        for(int n=0;n<sampleInfor.mainScanCount;n++)
        {
            QList<ScanPara> scanParas;
            pExperiment->GetScanPara(scanParas,1);
            QColor color=sampleInfor.GetMainScanResultPtr(n)->scanState.color;
            bool state=sampleInfor.GetMainScanResultPtr(n)->scanState.bIsShow;
            rawDatas=sampleInfor.GetMainScanResultPtr(n)->GetMainExpRawData();
            int index=0;
            for(int i=0;i<scanParas.count();i++)
            {
                int channel=scanParas.at(i).channels;
                double startMass=scanParas.at(i).startMassNum;
                double interval=scanParas.at(i).interval;

                QwtPlotCurve *pCurve = new QwtPlotCurve;
                pCurve->setRenderHint( QwtPlotItem::RenderAntialiased, true );
                pCurve->attach(pQwtPlotSpectral);
                pCurve->setVisible(state);
                curves.append(pCurve);

                pCurve->setPen(color);
                pCurve->setStyle(QwtPlotCurve::Lines);
                //
                QList<double> xs=Algorithm::GetNumList(channel,startMass,interval);
                QList<double> ys;
                for(int j=0;j<channel;j++)
                {
                    if(index>=rawDatas.count()) break;
                    ys.append(rawDatas.at(index).plusedValue);
                    //ys.append(rawDatas.at(index).analogValue);
                    index++;
                }
                pCurve->setSamples(xs.toVector(),ys.toVector());
            }
        }
    }

    pQwtPlotSpectral->replot();
}
